<?php

/**
 * This class manages all HTTP requests concerning children.
 */
class ChildController extends Zend_Controller_Action
{

    protected $_namespace = 'ChildController';

    public function init()
    {
	$this->_helper->acl->allow('admin')
		->allow('user', array('index', 'list'));
    }

    /**
     * Show all children.
     */
    public function indexAction()
    {
	$this->listAction();
    }

    /**
     * Show all children.
     */
    public function listAction()
    {
	$m = new Model_Child;
	;
	$children_aktiv = $m->listAllAktiv();
	$children_inaktiv = $m->listAllInAktiv();
	$this->view->childrenaktiv = $children_aktiv;
	$this->view->childreninaktiv = $children_inaktiv;
	$this->view->childrenCount = count($children_inaktiv);

	
	
    }

    /**
     * Create a new child.
     * 
     * Without POST data, an empty form is displayed.
     * With valid POST data, the child is persisted and one gets redirected
     * to the listAction.
     * With invalid POST data, the form's values with error messages is displayed.
     */
    public function createAction()
    {
	$request = $this->getRequest();
	/* $request_from => which page calls
	 * 0 = child_list -> create new child -> Child_list
	 * 1 = create a new application -> create new application -> new_application_report/id
	 */
	$request_from = (int) $request->getParam('from');
	$form = new Application_Form_Child();
	$form->addElement('submit', 'submit', array(
	    'ignore' => true,
	    'label' => 'Kind anlegen',
	));

	if ($request->isPost())
	{
	    if ($form->isValid($request->getPost()))
	    {
		$child = $form->getValues();
		$date = new Zend_Date($child['birthday'], 'dd.MM.yyyy');
		$child['birthday'] = $date->get('yyyy-MM-dd');
		$m = new Model_Child;
		$id = $m->create($child);

		// request come from create Request
		if ($request_from == 0)
		{
		    return $this->_helper->redirector('list');
		}
		// request come from create new application
		if ($request_from == 1)
		{
		    return $this->_helper->redirector->gotoURL('/child/report/id/' . $id);
		}
	    }
	}
	$this->view->form = $form;
    }

    /**
     * Update data of an existing child.
     * 
     * URL parameter id<int> is required.
     * 
     * Without POST data, a form is with the child's data is displayed.
     * With valid POST data, the child is persisted and the user is redirected
     * to /child/list.
     * With invalid POST data, the form's values with error messages is displayed.
     */
    public function updateAction()
    {
	/* $request_from => which page calls
	 * 0 = child_liste -> Child_list
	 * 1 = create a new application/id -> child_report/id
	 * 2 = child report/id -> child_report/id
	 */
	$request = $this->getRequest();
	$id = (int) $request->getParam('id');
	$request_from = (int) $request->getParam('from');

	$form = new Application_Form_Child();
	$form->addElement('submit', 'update', array(
	    'ignore' => true,
	    'label' => 'Änderung speichern',
	));

	$m = new Model_Child;

	if ($request->isPost())
	{
	    if ($form->isValid($request->getPost()))
	    {
		$child = $form->getValues();
		$child['id'] = $id;

		$date = new Zend_Date($child['birthday'], 'dd.MM.yyyy');
		$child['birthday'] = $date->get('yyyy-MM-dd');
		;
		$m->update($child);

		// request come from create Request
		if ($request_from == 0)
		{
		    return $this->_helper->redirector('list');
		}
		// request come from create new application
		if ($request_from == 1)
		{
		    return $this->_helper->redirector->gotoURL('/child/report/id/' . $id);
		}
		// request come from report child
		if ($request_from == 2)
		{
		    return $this->_helper->redirector->gotoURL('/child/report/id/' . $id);
		}
	    }
	} else
	{
	    $child = $m->find($id);
	    $birthday = new Zend_Date($child['birthday'], 'yyyy-MM-dd');
	    $child['birthday'] = $birthday->get("dd.MM.yyyy");
	    $form->populate($child);
	}

	$this->view->form = $form;
    }

    /**
     * Remove an existing child.
     * 
     * URL parameter id<int> is required. The user is redirected to /child/list
     */
    public function deleteAction()
    {
	$request = $this->getRequest();
	$id = (int) $request->getParam('id');

	//delete all Entry in PersonLinkPerson with this id
	$a = new Model_Allocate;
	$a->allocate_deleteByPersonIdAction($id);
	//delete child in Person
	$m = new Model_Child;
	$m->delete($id);

	return $this->_helper->redirector('list');
    }

    public function reportAction()
    {

	$request = $this->getRequest();
	$child_id = (int) $request->getParam('id');
	// list child
	$m = new Model_Child;
	$child = $m->find($child_id);
	$child['age'] = $m->calcAge($child['birthday']);
	$this->view->children = $child;
	//list all parents of child
	$p = new Model_Parent;
	$parent = $p->listParentsOfChildWithId($child_id);
	$this->view->parents = $parent;
	$this->view->parentCount = count($parent);
	//list all event of child
	$a = new Model_Allocate;
	$event = $a->listALLeventsByPersonID($child_id, 0);
	$this->view->events = $event;
	$this->view->eventsCount = count($event);
	$eventold = $a->listALLeventsByPersonID($child_id, 1);
	$this->view->eventsold = $eventold;
	$this->view->eventsoldCount = count($eventold);

    }

    public function infoAction()
    {
	/* $request_from => which page calls
	 * 0 = child_liste -> Child_list
	 * 1 = create a new application/id -> child_report/id
	 * 2 = child report/id -> child_report/id
	 */
	$request = $this->getRequest();
	$id = (int) $request->getParam('id');
	$request_from = (int) $request->getParam('from');


	$m = new Model_Child;
	$child = $m->find($id);
	$birthday = new Zend_Date($child['birthday'], 'yyyy-MM-dd');
	$child['birthday'] = $birthday->get("dd.MM.yyyy");
	$child['age'] = $m->calcAge($child['birthday']);
	$this->view->children = $child;
	$this->view->request_from = $request_from;
	
	$p = new Model_Parent;
	$parent = $p->listParentsOfChildWithId($id);
	$this->view->parents = $parent;
	$this->view->parentCount = count($parent);
	
	$a = new Model_Allocate;
	$event = $a->listALLeventsByPersonID($id, 0);
	$this->view->events = $event;
	$this->view->eventsCount = count($event);
	
    }

    public function setinaktivAction()
    {
	/* $request_from => which page calls
	 * 0 = child_liste -> Child_list
	 * 1 = create a new application/id -> child_report/id
	 * 2 = child report/id -> child_report/id
	 */
	$request = $this->getRequest();
	$id = (int) $request->getParam('id');
	$request_from = (int) $request->getParam('from');

	$m = new Model_Child;
	$m->setinaktiv($id);

	// request come from create Request
	if ($request_from == 0)
	{
	    return $this->_helper->redirector('list');
	}
	// request come from create new application
	if ($request_from == 1)
	{
	    return $this->_helper->redirector->gotoURL('/child/report/id/' . $id);
	}
	// request come from report child
	if ($request_from == 2)
	{
	    return $this->_helper->redirector->gotoURL('/child/report/id/' . $id);
	}
    }

    public function setaktivAction()
    {
	/* $request_from => which page calls
	 * 0 = child_liste -> Child_list
	 * 1 = create a new application/id -> child_report/id
	 * 2 = child report/id -> child_report/id
	 */
	$request = $this->getRequest();
	$id = (int) $request->getParam('id');
	$request_from = (int) $request->getParam('from');

	$m = new Model_Child;
	$m->setaktiv($id);

	// request come from create Request
	if ($request_from == 0)
	{
	    return $this->_helper->redirector('list');
	}
	// request come from create new application
	if ($request_from == 1)
	{
	    return $this->_helper->redirector->gotoURL('/child/report/id/' . $id);
	}
	// request come from report child
	if ($request_from == 2)
	{
	    return $this->_helper->redirector->gotoURL('/child/report/id/' . $id);
	}
    }

    public function forwardAction()
    {
	$request = $this->getRequest();

	if ($request->getParam('from') != NULL && $request->getParam('url') != NULL)
	{

	    $session = $this->_session = new Zend_Session_Namespace('redirect');

	    $session->url = $request->getParam('from');

	    $this->redirect($request->getParam('url'));
	}
	$this->redirect('/child/list');
    }


}